<?php

class FirmmediaController extends AdminController {


	public function actionView($id) {
		$this->render('view', array(
			'model' => $this->loadModel($id, 'FirmMedia'),
		));
	}

	public function actionCreate() {
		$model = new FirmMedia;

		if (isset($_POST['FirmMedia'])) {
            if (isset($_POST['Firms']['name']) && '' == $_POST['Firms']['name']) unset($_POST['Firms']['name']);
			$model->setAttributes($_POST['FirmMedia']);
            if (1 == $model->media_type) {
                $old_img_name = $model->name;
                $upload_data = $this->uploadFirmImg($model, 'name', $model->firm_id);
                $model->name = $upload_data['img_name'];
                $resize = $upload_data['success'];            
            }
            
            
			if ($model->save()) {
                if (1 == $model->media_type) {
                    if ($resize) {
                        $this->resizeFirmImg($model->name, array(
                                'firms_img_thumb' => array('w'=>$this->getYiiP('firms_thumb_w'), 'h'=>$this->getYiiP('firms_thumb_h')),
                                'firms_img_mid' => array('w'=>$this->getYiiP('firms_mid_w'), 'h'=>$this->getYiiP('firms_mid_h')),
                                'firms_img_big' => array('w'=>$this->getYiiP('firms_big_w'), 'h'=>$this->getYiiP('firms_big_h')),
                            )
                        );
                    }                  
                }
                
				if (Yii::app()->getRequest()->getIsAjaxRequest())
					Yii::app()->end();
				else
					$this->redirect(array('view', 'id' => $model->id));
			}
		}

		$this->render('create', array( 'model' => $model));
	}

	public function actionUpdate($id) {
		$model = $this->loadModel($id, 'FirmMedia');


		if (isset($_POST['FirmMedia'])) {
            //removing empty logo field
            if (isset($_POST['Firms']['logo']) && '' == $_POST['Firms']['logo']) unset($_POST['Firms']['logo']);
        
			$model->setAttributes($_POST['FirmMedia']);
            if (1 == $model->media_type) {
                $old_img_name = $model->name;
                $upload_data = $this->uploadFirmImg($model, 'name', $model->firm_id);
                $model->name = $upload_data['img_name'];
                $resize = $upload_data['success'];     
            }
			if ($model->save()) {
                if (1 == $model->media_type) {
                    if ($resize) {
                        $this->resizeFirmImg($model->name, array(
                                'firms_img_thumb' => array('w'=>$this->getYiiP('firms_thumb_w'), 'h'=>$this->getYiiP('firms_thumb_h')),
                                'firms_img_mid' => array('w'=>$this->getYiiP('firms_mid_w'), 'h'=>$this->getYiiP('firms_mid_h')),
                                'firms_img_big' => array('w'=>$this->getYiiP('firms_big_w'), 'h'=>$this->getYiiP('firms_big_h')),
                            )
                        );
                    }                  
                }            
				$this->redirect(array('view', 'id' => $model->id));
			}
		}

		$this->render('update', array(
				'model' => $model,
				));
	}

	public function actionDelete($id) {
		if (Yii::app()->getRequest()->getIsPostRequest()) {
            $model = $this->loadModel($id, 'FirmMedia');
            // $this->deleteImg($model->name);
            // $model->delete();
			$model->deleted = 1;
			if ($model->save()) {
				if (!Yii::app()->getRequest()->getIsAjaxRequest()) $this->redirect(array('admin'));
			}	

		} else
			throw new CHttpException(400, AdminModule::t('Некорректный запрос.'));
	}

	//????
	public function actionDeleteImg() {
		if (!Yii::app()->getRequest()->getIsAjaxRequest()) $this->redirect(array('admin'));
		$record_id = intval(CHttpRequest::getParam('id'));
		if (0 >= $record_id) $this->ajax_responce('error:record_id');
        $model = $this->loadModel($record_id, 'FirmMedia');
		if (!$this->deleteImg($model->name)) $this->ajax_responce('error:deleting');
        $model->name = '';
        $model->save();
		return $this->ajax_responce('success');
	}	    
    
	protected function deleteImg($name) {
        if ('' == $name) return false;
        @unlink($this->getFirmImgSrc($name, 'firms_img_thumb', 'real'));
        @unlink($this->getFirmImgSrc($name, 'firms_img_mid', 'real'));
        @unlink($this->getFirmImgSrc($name, 'firms_img_big', 'real'));
		@unlink($this->getFirmImgSrc($name, 'firms_img_orig', 'real'));
		return true;
	}      
    
    
    
	public function actionIndex() {
		$dataProvider = new CActiveDataProvider('FirmMedia');
		$this->render('index', array(
			'dataProvider' => $dataProvider,
		));
	}

	public function actionAdmin() {
		$model = new FirmMedia('search');
		$model->unsetAttributes();
		$model->deleted = 0;
		if (isset($_GET['FirmMedia']))
			$model->setAttributes($_GET['FirmMedia']);

		$this->render('admin', array(
			'model' => $model,
		));
	}

    
    
    
}